package com.ocom.webapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.dto.Department;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.request.web.AccDiscountEnum;
import com.ocom.common.request.web.AccDiscountReportsOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.recode.GetAccDiscountReportsVo;
import com.ocom.common.vo.webapi.MealListVo;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.feign.XfRecordFeign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')")
public class AccDiscountReportsController {

    @Autowired
    private HumanClientService humanClientService;

    @Autowired
    private XfRecordFeign xfRecordFeign;

    @Autowired
    private PayConfigClientService payConfigClientService;


    @PostMapping(WebApiServiceUrl.URL_AccDiscountReportsOnShow)
    public Result accDiscountReportsOnShow(@RequestBody AccDiscountReportsOnShowRequest request) {
        String deptIds = "";
        if (null != request.getDeptId() && request.getDeptId() > 0) {
            Department department = new Department();
            department.setComId(request.getComId());
            department.setDeptId(Long.valueOf(request.getDeptId()));
            department.setShowLevel(0);
            Result deptResult = humanClientService.getDeptIds(department);
            if (deptResult.getCode() == 200) {
                JSONObject deptJson = JSON.parseObject(JSON.toJSONString(deptResult.getData()));
                deptIds = deptJson.getString("deptIds");
                if (StringUtils.isNotBlank(deptIds)) {
                    deptIds = request.getDeptId() + "," + deptIds;
                } else {
                    deptIds = request.getDeptId().toString();
                }
            }
        }
        request.setDeptIds(deptIds);

        Map<String, Object> returnMap = new HashMap<>();

        for (AccDiscountEnum value : AccDiscountEnum.values()) {
            request.setType(value);
            Result accDiscountReports = xfRecordFeign.getAccDiscountReports(request);
            List<GetAccDiscountReportsVo> getAccDiscountReportsVos = new ArrayList<>();
            if (200 == accDiscountReports.getCode()) {
                getAccDiscountReportsVos = JSON.parseArray(JSON.toJSONString(accDiscountReports.getData()), GetAccDiscountReportsVo.class);
            }
            switch (request.getType()) {
                case DIS_DATA:
                    HashMap<String, Integer> map = new HashMap<>();
                    for (GetAccDiscountReportsVo vo : getAccDiscountReportsVos) {
                        String mealDate = vo.getMealDate()+vo.getAccTypeId();
                        if(request.getReportType()==3){
                            mealDate = vo.getMealDate().substring(0,4)+vo.getAccTypeId();
                        }
                        Integer i = map.get(mealDate);
                        if (i == null) {
                            i = 1;
                        } else {
                            i = i + 1;
                        }
                        String disNum = "第" + i + "餐";
                        vo.setDisNum(disNum);
                        map.put(mealDate, i);
                    }

                    returnMap.put(value.getValue(), getAccDiscountReportsVos);
                    break;
                case MEAL:

                    Result mealList = payConfigClientService.GetMealNameInfo(request.getComId());
                    List<EntityXfMealInfo> xfMealInfos = new ArrayList<>();
                    if (mealList.getCode() == 200) {
                        MealListVo mealListVo = JSON.parseObject(JSON.toJSONString(mealList.getData()), MealListVo.class);
                        xfMealInfos = mealListVo.getList();
                    }
                    returnMap.put(value.getValue(), xfMealInfos);
                    break;
                case ACC_TYPE:
                    Result accountTypeList = payConfigClientService.GetXFAccountType(request.getComId());
                    List<EntityXfAccountType> xfAccountTypes = new ArrayList<>();
                    if (accountTypeList.getCode() == 200) {
                        xfAccountTypes = JSON.parseArray(JSON.toJSONString(accountTypeList.getData()), EntityXfAccountType.class);
                        EntityXfAccountType entityXfAccountType =new EntityXfAccountType();
                        entityXfAccountType.setAccTypeId(0);
                        entityXfAccountType.setAccTypeName("其他");
                        xfAccountTypes.add(entityXfAccountType);
                    }
                    returnMap.put(value.getValue(), xfAccountTypes);
                    break;
                default:
                    returnMap.put(value.getValue(), getAccDiscountReportsVos);
            }
        }

        return ResultUtil.success(returnMap);
    }

}
